package com.youhome.business.controller;

import com.youhome.business.dto.DTOOwnHouse;
import com.youhome.business.dto.DTOVideoUpload;
import com.youhome.common.exception.CustomException;
import com.youhome.common.response.Result;
import com.youhome.common.response.ResultCode;
import com.youhome.service.model.HousePropertyType;
import com.youhome.service.parameter.ParamOwnHouse;
import com.youhome.service.service.*;
import io.swagger.annotations.*;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by chen365 on 2018/05/29.
 */
@RestController
@RequestMapping(value = "/own")
@Api(tags = "自有房源")
public class OwnHomeController {

    @Autowired
    private ListHubPhotoService listHubPhotoService;
    @Autowired
    private HouseCommonService houseCommonService;
    @Autowired
    private ListHubService listHubService;
    @Autowired
    private VideoTransCodingService videoTransCodingService;
    @Autowired
    private HouseVideoService houseVideoService;


    @ApiOperation(value = "房屋一级类型", notes = "房屋一级类型", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping(value = "/property/type")
    public Object PropertyType() {
        List<HousePropertyType> list = houseCommonService.selectHousePropertyTypeAll();
        return new Result(ResultCode.SUCCESS, list);
    }

    @ApiOperation(value = "保存房源", notes = "保存房源", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PostMapping(value = "/save")
    public Object save(@ApiParam @Validated DTOOwnHouse dtoOwnHouse) throws CustomException {
        ModelMapper modelMapper = new ModelMapper();
        listHubService.save(modelMapper.map(dtoOwnHouse, ParamOwnHouse.class));
        return new Result(ResultCode.SUCCESS);
    }

    @ApiOperation(value = "房屋二级类型", notes = "房屋二级类型", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping(value = "/property/subtype")
    public Object propertySubType() {
        return new Result(ResultCode.SUCCESS, houseCommonService.selectHousePropertySubTypeAll());
    }

    @ApiOperation(value = "50州", notes = "50州", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping(value = "/property/state")
    public Object propertyState() {
        return new Result(ResultCode.SUCCESS, houseCommonService.selectStateAll());
    }

    @ApiOperation(value = "删除房源", notes = "删除房源", response = Result.class, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "houseId", required = true, paramType = "path")
    })
    @DeleteMapping(value = "/delete/{houseId}")
    public Object delete(@PathVariable("houseId") String houseId) throws CustomException {
        listHubService.delete(houseId);
        return new Result(ResultCode.SUCCESS);
    }

    @ApiOperation(value = "设置明星房源", notes = "设置明星房源", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "houseId", required = true, paramType = "path")
    })
    @PostMapping(value = "/star/{houseId}")
    public Object star(@PathVariable String houseId) throws CustomException {
        listHubService.setStar(houseId);
        return new Result(ResultCode.SUCCESS);
    }

    @ApiOperation(value = "删除房源图片", notes = "删除房源图片", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源ID", required = true, paramType = "path"),
            @ApiImplicitParam(dataType = "String", name = "ossUrl", value = "图片URL", required = true, paramType = "query"),
    })
    @DeleteMapping(value = "/delete/photo/{houseId}")
    public Object deletePhoto(@PathVariable String houseId, String ossUrl) throws CustomException {
        listHubPhotoService.deletePhoto(houseId, ossUrl);
        return new Result(ResultCode.SUCCESS);
    }

    @ApiOperation(value = "上传房源视频", notes = "上传房源视频", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PostMapping(value = "/video")
    public Object uploadVideo(@ApiParam @Validated DTOVideoUpload dtoVideoUpload) throws CustomException {
        videoTransCodingService.transCoding(dtoVideoUpload.getHouseId(), dtoVideoUpload.getName(), dtoVideoUpload.getSize(), dtoVideoUpload.getUrl());
        return new Result<>(ResultCode.SUCCESS);
    }

    @ApiOperation(value = "获取房源视频", notes = "获取房源视频", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "houseId", value = "房源ID", required = true, paramType = "query"),
    })
    @GetMapping(value = "/video")
    public Object videoList(String houseId) throws CustomException {
        return new Result<>(ResultCode.SUCCESS, houseVideoService.selectVideoByHouseId(houseId));
    }

    @ApiOperation(value = "删除房源视频", notes = "删除房源视频", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "String", name = "videoId", value = "视频ID", required = true, paramType = "query"),
    })
    @DeleteMapping(value = "/video")
    public Object deleteVideo(String videoId) throws CustomException {
        houseVideoService.deleteByEnabled(videoId);
        return new Result<>(ResultCode.SUCCESS);
    }

}
